package com.example.vue20220225.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue20220225.dao.PowerDao;
import com.example.vue20220225.dao.RoleDao;
import com.example.vue20220225.model.Power;
import com.example.vue20220225.model.UserInfo;
import com.example.vue20220225.service.PowerService;
import com.example.vue20220225.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PowerServiceImpl implements PowerService {

    @Resource
    PowerDao powerDao;

    @Override
    public Map<String, Object> querypowerlist(Power power) {
        Map<String, Object> map = new HashMap<>();
        //  从前端传回  页码  和  每页显示条数
        Page<Power> page = new Page<>(power.getPage(),power.getRow());
        QueryWrapper<Power> queryWrapper = new QueryWrapper<>();
        if(power.getPowerName() != null && !power.getPowerName().equals("")){
            queryWrapper.like("power_name",power.getPowerName());
        }
        Page<Power> list = powerDao.selectPage(page, queryWrapper);
        //  getRecords  查询的 分页 用户数据
        List<Power> powers = list.getRecords();
        //  把查询出来的 数据  放到map中  key是users
        map.put("powers",powers);
        //  数据 最大条数  有多少条数据
        map.put("maxcount",list.getTotal());
        return map;
    }

    @Override
    public Map<String, Object> getFirstPowerlist() {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<Power> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        List<Power> powers = powerDao.selectList(queryWrapper);
        map.put("powers",powers);
        return map;
    }

    @Override
    public Map<String, Object> addpower(Power power) {
        Map<String, Object> map = new HashMap<>();
        power.setPowerId(null);
        int flag = powerDao.insert(power);
        if(flag < 1){
            map.put("message","新增失败");
        }else{
            map.put("message","新增成功");
        }
        return map;
    }

    @Override
    public Map<String, Object> editpower(Power power) {
        Map<String, Object> map = new HashMap<>();
        int flag = powerDao.updateById(power);
        if(flag < 1){
            map.put("message","修改失败");
        }else{
            map.put("message","修改成功");
        }
        return map;
    }

    @Override
    @Transactional
    public Map<String, Object> deletepower(Power power) {
        Map<String, Object> map = new HashMap<>();
        int flag = 0;
        try {
            // 删除关联表
            powerDao.deletePowerRole(power.getPowerId());
            // 删除父类和子类 power 表
            powerDao.deleteFatherAndChild(power);
            flag = 1;
        }catch (Exception e){
            e.printStackTrace();
        }
        if(flag > 0){
            map.put("message","删除成功");
        }else{
            map.put("message","删除失败");
        }
        return map;
    }
}
